﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MarchEstate.Manager.BusinesEntity;
using MarchEstate.Manager.Dao.Room;
using CoreFrame.Core.Aop;
using CoreFrame.Core.Transaction;

namespace MarchEstate.Manager.ObjectModel.Room
{
    public class CustomerManager : AbstractCustomer, IJoinManager
    {
        private AreaRoomDao areaRoomDao = null;

        public CustomerManager()
        {

        }
        public override void InitDao()
        {
            base.InitDao();
            areaRoomDao = NewDao<AreaRoomDao>();
        }

        #region IJoinManager 成员
        [AOP(typeof(AopTransaction))]
        public bool Join(System.Collections.Hashtable value)
        {
            //如果不是选择的人员则新增人员
            AbstractOwner owner = NewObject<AbstractOwner>();
            if (value["ownerId"].ToString().Trim() == "")
            {
                owner.Name = value["OwnerName"].ToString();
                owner.OftenMobile = value["tel"].ToString();
                owner.save();
            }
            //添加客户
            AbstractCustomer customer = NewObject<AbstractCustomer>();
            customer.CustomerName = value["customer_name"].ToString();
            customer.OwnerId = owner.OwnerId;
            customer.OwnerName = value["OwnerName"].ToString();
            customer.JoinFlag = 1;
            customer.JoinDate = DateTime.Now.Date;
            customer.save();
            if (value["itemcode"].ToString().Trim() != "")
            {
                //添加项目对应关系表
                AbstractServiceItem item = NewObject<AbstractServiceItem>();
                AbstractServiceItem _item = item.GetServiceItem(value["itemcode"].ToString());
                AbstractOSRCCItemRelation _srcc = NewObject<AbstractOSRCCItemRelation>();
                _srcc.OwnerId = owner.OwnerId;
                _srcc.SrccId = customer.CustomerId;
                _srcc.ItemCode = _item.Code;
                _srcc.ItemName = _item.ItemName;
                //如果绑定的项目已存在就不增加重复的
                _srcc.save();
                //初始化默认值
                _srcc.UpdateThisFee(_srcc.SrccId, _srcc.ItemCode, Convert.ToDateTime(value["maturity"]).Date, Convert.ToDateTime(value["maturity"]).Date, Convert.ToDecimal(value["Lastsurplus"]), Convert.ToDecimal(value["LastLateFee"]));
            }
            return true;
        }
        [AOP(typeof(AopTransaction))]
        public bool CancelJoin(string srccId)
        {
            AbstractOSRCCItemRelation _srcc = NewObject<AbstractOSRCCItemRelation>();
            _srcc.Delete(srccId, null);
            return areaRoomDao.CancelJoinCustomer(srccId);
        }

        #endregion
    }
}
